<?php

include_once '../utils/constants.php';
include_once '../utils/debug.php';
include_once 'race.php';

class Races {

	/**
	 * array with Race objects
	 */
	var $races = array();

	public function Races() {
		if (DEBUG) enex("Races::Races()");
	}

	public function buildFromFile($filename) {
		if (DEBUG) enter("Races::buildFromFile($filename)");
		$content = file_get_contents($filename);
		$raceStringArray = split("\n", $content);
		foreach ($raceStringArray as $raceString) {
			$race = new Race();
			array_push($this->races, $race->buildFromString($raceString));
		}
		if (DEBUG) ex("Races::buildFromFile($filename)");
		return $this;
	}

	public function buildFromFileLimit($filename, $idArray) {
		if (DEBUG) enter("Races::buildFromFileLimit()");
		$content = file_get_contents($filename);
		$raceStringArray = split("\n", $content);
		foreach ($raceStringArray as $raceString) {
			list($raceId) = split(" ", $raceString);
			$res = array_search($raceId, $idArray, TRUE);
			if($res !== false) {
				$race = new Race();
				array_push($this->races, $race->buildFromString($raceString));
			}
		}
		if (DEBUG) ex("Races::buildFromFileLimit()");
		return $this;
	}

	public function generateId() {
		if (DEBUG) enex("Races::generateId()");
		return getMaxId(RACES) + 1;
	}
	
	public function getRaces() {
		if (DEBUG) enex("Races::getRaces()");
		return $this->races;
	}
	
	public function addRace($race) {
		array_push($this->races, $race);
	}
}

?>